<?php

namespace App\Console\Commands;

use App\Http\Repositories\Eloquent\Brand;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class copyBrand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'copy:brand';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $data = DB::table('zb_vehicle')->orderBy('v_id','desc')->get();
        if ($data->count()) {
            foreach ($data as $key => $item) {
                $exists = DB::table('brands')->where('name',$item->v_name)->exists();
                if ($exists) {
                    continue;
                }
                $dao = new Brand();
                $dao->name = $item->v_name;
                $dao->first = $item->v_s;
                $dao->parent_id = 0;
                $dao->save();
                $parentId = $dao->id;
                $lower = DB::table('zb_vehicle_xh')->where('v_id',$item->v_id)->get();
                if ($lower->count()) {
                    foreach ($lower as $k => $v) {
                        $dao = new Brand();
                        $dao->name = $v->vx_name;
                        $dao->first = '';
                        $dao->parent_id = $parentId;
                        $dao->save();
                    }
                }

            }
        }

    }
}
